$(function(){
	var information={};//传给后台的json
	var value=[];
	var data=[];
	var childer2=[];  
	var childer=[];   
	//文件上传按钮
 
	$("#fileUp").click(function(){
		
		$("#loading").hide();
		$("#loadOk").hide();
	})
	$("#fileUp").on("change","#upfile-haoping",function(){
		fileup();
	})
	
	
	$("#sure").click(function(){
		setSubmit();
	});
	
	//提交按钮，弹出提交模态框
	$("#submitInfo").click(function(){
		
		sure();	   
	});
	//所有数据，和上一层id

	function xxx(object,className){
		var type="";
		var name="";
		if(className=="#treeTable1")
		{
			type="checkbox";
			name="sourceCheck";
		}
		else
		{
			type="radio";
			name="safePort";
		}
		var a=[];
		var flag=0;
			var html="";
		var flaga={};
		
		function treeMenu(a){
		    this.tree=a||[];
		    this.groups={};
		};
		treeMenu.prototype={
		    init:function(superId){
		  
		        this.group();
		        return this.getDom(this.groups[superId]);
		    },
		    group:function(){
		    
		        for(var i=0;i<this.tree.length;i++){
		            if(this.groups[this.tree[i].superId]){
		                this.groups[this.tree[i].superId].push(this.tree[i]);
		            }else{
		                this.groups[this.tree[i].superId]=[];
		                this.groups[this.tree[i].superId].push(this.tree[i]);
		            }
		        }

		    },

		    getDom:function(a){
		    	
		    	
		        if(!a){return ''}
		       flag=0;
		        var html="";
		    
		    	if(a[0].superId==0)
		    	{
		    			/*$scope.sorce=a;//第一层结构
		    			//console.log($scope.sorce);*/
		    			for(var i=0;i<a.length;i++)
		    			{
		    				
		    				var html='<tr isChange=false id="'+a[i].id+'"   hasChild="hasChild" data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td><input data-name='+a[i].name+' data-id='+a[i].id+' type='+type+' name='+name+'></td></tr>\n';		
		    				$(className).append(html);
		    				
		    			}
		    			
		    			flag=1;
		    	}
		    	////console.log("aa"+JSON.stringify(a));
		    	if(flag==0)
		    	{
		    			var length=a.length;
		    			var superid=0;
		    			for(var i=0;i<length;i++)
		    			{	
		    				superid=a[i].superId;
		    				if(isChilde(a[i].id,object))
		    				{ 
		    					html+='<tr isChange=false id="'+a[i].id+'" hasChild="hasChild" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td><input data-name='+a[i].name+' data-id='+a[i].id+' type='+type+' name='+name+'></td> </tr>\n';		
		    				}  
		    				else
		    				{  			
		    					html+='<tr isChange=false id="'+a[i].id+'" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td><input data-name='+a[i].name+' data-id='+a[i].id+' type='+type+' name='+name+'></td></tr>\n';		
		    				}
		    				
		    				
		    				    
		    			}
		    				if(className=="#treeTable1")
		    				{
		    					childer.push({superid:superid,html:html});
		    				}
		    				else
		    				{
		    					childer2.push({superid:superid,html:html});
		    				}

		    			
		    		
		    		
		    			
		    	}
		    	 
		        for(var i=0;i<a.length;i++)
		        {
		           this.getDom(this.groups[a[i].id]);

		        }

	        /*html+='</optgroup>\n';*/

	       /* $("#"+id).append(html);*/
	        return html;
	    }
	   

	}
 var html=new treeMenu(object).init(0);
option(className);
}
	




//判断是否有子节点
function isChilde(id,object)
{
	for(var i=0;i<object.length;i++)
	{
		if(object[i].superId==id)
		{
			return true;
		}
	}
	return false;
}
	function option(className)
	{			
			var option = {
	                theme:'vsStyle',
	              
	                beforeExpand : function($treeTable, id) {

	            	var html="";
	                    //判断id是否已经有了孩子节点，如果有了就不再加载，这样就可以起到缓存的作用
	                    if ($('.' + id, $treeTable).length) { return; }
	            
	                   var index=$(className).find("tr").eq(id).attr("id");
	                   if(className=="#treeTable2")
		    			{
		                   for(var i=0;i<childer2.length;i++)
		                   {
		                   	if(id==childer2[i].superid)
		                   	{
		                   		html=childer2[i].html;
		                   	}
		                   }
		                 }
		                 else
		                 {
		                 	 for(var i=0;i<childer.length;i++)
		                   	{
		                   		if(id==childer[i].superid)
		                   		{
		                   			html=childer[i].html;
		                   		}
		                  	 }
		                 }
	                
	                     $treeTable.addChilds(html);
					},
					 onSelect : function($treeTable, id) {
	                    window.console && console.log('onSelect:' + id);
	                    
	                }


		}	
	
		$(className).treeTable(option);
	}


var x=1;	//向后台请求渠道信息
function space(superid,object)
{

	if(superid==0)
	{

		return 0;
	}
	for(var i=0;i<object.length;i++)
	{
		if(object[i].id==superid)
		{
			
			x=space(object[i].superId,object);
			
		}
	}
	return x+1;

}
function requestSource()
{	


	var url="getAllInfomationSource.do";//渠道地址
	var data={};
	var object={};
	var superId1=[];//第一层id
	YuXi.util.get(url,"null",function(data){
		object=data.object;
		 object=[
			{id:1,name:"上级渠道",superId:0,remark:"xxxxsd",level:1},
			{id:2 ,name:"监狱渠道",superId:0,remark:"xxxxsd",level:2},
			{id:3 ,name:"个人渠道",superId:0,remark:"xxxxsd",level:3},
			{id:4 ,name:"其他渠道",superId:0,remark:"xxxxsd",level:4},
			
			{id:5 ,name:"警务督察",superId:1,remark:"xxxxsd",level:5},
			{id:6 ,name:"巡逻检查",superId:1,remark:"xxxxsd",level:6},
			{id:7 ,name:"日常考核",superId:1,remark:"xxxxsd",level:7},
			{id:8 ,name:"集中考核",superId:1,remark:"xxxxsd",level:8},
			

			
			{id:16 ,name:"巡逻检查",superId:2,remark:"xxxxsd",level:2},
			{id:17 ,name:"日搜日结",superId:2,remark:"xxxxsd",level:2},
			{id:18 ,name:"其他类",superId:2,remark:"xxxxsd",level:2},

			{id:19 ,name:"犯罪回报条",superId:7,remark:"xxxxsd",level:2},
			{id:20,name:"犯罪回报本",superId:7,remark:"xxxxsd",level:2},

			{id:21,name:"会见监听",superId:20,remark:"xxxxsd",level:2}];
	xxx(object,"#treeTable1");
	
		
	},function(){});//请求数据



}

	//向后台提交附件，返回地址
	 function fileup (){
			$("#loading").show();
			 $("#loadOk").hide();
	    $.ajaxFileUpload({
	        type:"post",
	        secureuri:false,
	        dataType:"json",      
	        url:"/yuxi/userFileUp.do",
	        fileElementId:"upfile-haoping",
	        success:function(data, status){
	        	//console.log(data);
	        	information.appendix=JSON.stringify(data.addressList);
	        	$("#loading").hide();
	        	$("#loadOk").show();
	        	$("#fileValue").remove();
	        	var html='<input id="upfile-haoping" name="files" style="position:absolute; right:0; top:0; font-size:100px; opacity:0; filter:alpha(opacity=0);" type="file" value="浏览" />';
	        	$("#fileUp").append(html);
	        },
	        error:function(data){
	        	
	          
	        } 
	        
	      });
	    }





	



function addUser(user,id){
	var html= '<li><input type="checkbox"  data-id='+id+' value='+user+' name="Check">'+user+'</li>';
	$("#addRequest").find("ul").append(html);
}
 function queryUser(id){
	 var structId="structId="+id;
	 var url="getUserListByStruct.do";//获取部门下的角色
	 var data={};
	 var object={}; 
	 $(".selctUser").html("");
	 data=YuXi.util.get(url,structId,function(data){
		object=data.object;
		for(var i=0;i<object.length;i++)
		{	
			var html= '<li><input type="checkbox" data-id='+object[i].userId+' data-na='+object[i].userName+' name="isCheck">'+object[i].userName+'</li>';
			$(".selctUser").append(html);
		}
			
	},function(){});
 }
 

//向后台提交所有数据，确认提交。(调用此方法需要给json数据)
function requestSubmit(information)
{	
	var url="addInformation.do";//提交地址
//	var data={};
	var object={};
	YuXi.util.post(url,information,function(data){
		if(data.status){
			$("#successmodel").modal('show');
			//console.log("提交成功");
			window.location.href="submittedInfo.jsp?menuId=1";
		}else{
			//console.log("提交失败");
		}
	},function(){
		//console.log("提交失败");
	});
}


//确认提交--获取页面值
function sure()
{	
	/*渠道*/

    var check=[];
    var sourceName=[];
    var personId=[];
    var personName=[];
   
    $(".select-data-user").each(function(){
    	//console.log("ID:"+$(this).attr("data-id")+$(this).html());
		personId.push($(this).attr("data-id"));
		personName.push($(this).html());
		
		
	})
	portId=$(".sele option:selected").val()+",";
	var sourceid="";
	var sourcename="";
	 $('input:checkbox[name=sourceCheck]:checked').each(function(i){
	 		sourceid=sourceid+$(this).attr("data-id")+",";
	 		sourcename=sourcename+$(this).attr("data-name")+",";
	 })
	
    information.informationSourceID=sourceid;//渠道
    information.informationSourceNames=sourcename;
    information.safetyPortId=$("input[type='radio']:checked").attr("data-id");; //分口
    information.safetyPortNames=$("input[type='radio']:checked").attr("data-name");
    information.informationClassificationId=parseInt($(".pes option:selected").val());//分级
    information.informationClassificationNames=($(".pes option:selected").html());
    information.informationSortId=($(".sort option:selected").val());//分类
    information.informationSortNames=($(".sort option:selected").html());
    information.informationProfile=$("#brie").val();//简介

    information.mesure=$(".step").val();//措施
    information.standard=$(".basic").val();//标准
    information.informationConternt=$(".cont").val();//内容
    information.inputPersonID=personId.join(",");//参与人
    information.inputPerSonNames=personName.join(",");
   
    information.prisonId=$("#infor-where-select option:checked").val();
    information.prisonName=$("#infor-where-select option:checked").attr("data-name");
   //console.log(JSON.stringify(information));
    if(!isNull(information.informationSourceID)){
    	myTool.alert({		
			title:"提示",
			context:"提交失败,渠道不能为空。",
			isMask:true,
			callback:function(){
				}
			});

    	YuXi.scrollbar.scroll("#resour");
    	return false;
    }
   
    if(!isNull(information.informationClassificationId)){
    	$("#badmodal").modal('show');
    	return false;
    }
    if(!isNull(information.informationSortId)){
    	$("#badmodal").modal('show');
    	return false;
    }  
    if(!isNull(information.informationProfile) || information.informationProfile=="" ){
    	
    	myTool.alert({		
			title:"提示",
			context:"提交失败,简介不能为空。",
			isMask:true,
			callback:function(){
				}
			});

    	YuXi.scrollbar.scroll("#brie");
    	return false;
    }
    if(!isNull(information.informationConternt) || information.informationConternt==""){
    	myTool.alert({		
			title:"提示",
			context:"提交失败,内容不能为空。",
			isMask:true,
			callback:function(){
				} 
			});

    	YuXi.scrollbar.scroll("#content");
    	return false;
    }
    $('#myModal').modal('show');
}
function setSubmit(){
	$('#myModal').modal('hide');
	requestSubmit(information);
}
//参与人搜索
function search()
{
	var search=$("#searchInsert").val();//需要查询的数据
	if(search!="")
	{
		requestSearch(search);
	}
}
//参与人选中后的确定按钮
function surePart()
{
	
	 var item=$(".ui-sortable .item");
     $("#node").find(".label1").html("");
     var pearson="";
     for(var i=0;i<item.length;i++)
     {
       firt=item[i].firstChild;
       if(i==item.length-1)
       {
         pearson+=firt.nextSibling.nodeValue;
       }
       else
       {
         pearson+=firt.nextSibling.nodeValue+",";

       }
       html="<span class='parsbox'><span  class='glyphicon glyphicon-user' aria-hidden='true'></span>"+firt.nextSibling.nodeValue+"&nbsp;&nbsp;</span></span>"
       $(".label1").append(html);
     }
     $('#myPart').modal('hide');
	
}
//cookie值获取和存储
function cookie(){
	if($.cookie("myData")!=null)
	{
		var data1=JSON.parse( $.cookie("myData"));
		//console.log("-------"+$.cookie("myData"));
		var check=$.cookie("myCheck").split(",");
		for(var i=0;i<check.length;i++)
		{
			$("#"+check[i]).attr("checked","checked");
		}
		$(".briefing").val(data1.briefing);
		$(".step").val(data1.step);
		$(".basic").val(data1.basic);
		$(".cont").val(data1.content);

		$(".sele option").eq(data1.sele-1).attr("selected","selected");
		$(".pes option").eq(data1.pes-1).attr("selected","selected");
		$(".sort option").eq(data1.sort-1).attr("selected","selected");
	}
		time=setInterval(function(){
			var check=[];
			
			for(var i=0;i<$(".check").length;i++)
			{
				if($(".check").eq(i).is(":checked"))
				{
					check.push($(".check").eq(i).attr('id'));
				}
			}
			var sele=1;
			var pes=1;
			var sort=1;
			sele=parseInt($(".sele option:selected").val());//分口
			pes=parseInt($(".pes option:selected").val());//分级
			sort=parseInt($(".sort option:selected").val());//分类
			//参与人------------------------
			var briefing=$(".briefing").val();//简介
			var step=$(".step").val();//措施
			var basic=$(".basic").val();//标准
			var content=$(".cont").val();//内容
			//附件------------------------
			var data={
				sele:sele,
				pes:pes,
				sort:sort,
				briefing:briefing,
				step:step,
				basic:basic,
				content:content
			};

			
			var dataStr=JSON.stringify(data);
			var checkStr=check.join(",");
			//console.log(dataStr+"长度"+dataStr.length);
			if(dataStr.length >80)
			{
				$.cookie("myCheck",checkStr,{ expires: 7 });
				$.cookie("myData",dataStr,{ expires: 7 });
			}
			else
			{
				clearInterval(time);
			}
		},3000)
}


$("#save").click(function(){
	$("#saveModal").modal('show');
})


/*判断输入合法性*/
function isNull(str){
	if(str==null||str==""){
		return false;
	}else{
		return true;
	}
}



//向后台请求分口信息
function requestPort()
{	var url="getAllInformationPort.do";//分口地址
	var data={};
	var object={};
	data=YuXi.util.get(url,null,function(data){
		object=data.object;
		var object=[
			{id:1,name:"分口1",superId:0,remark:"xxxxsd", level:1},
			{id:2 ,name:"分口1",superId:0,remark:"xxxxsd",level:2},
			{id:3 ,name:"分口1",superId:0,remark:"xxxxsd",level:4},
			{id:4 ,name:"分口1",superId:0,remark:"xxxxsd",level:3},
			
			{id:5 ,name:"分口1",superId:1,remark:"xxxxsd",level:1},
			{id:6 ,name:"分口1",superId:1,remark:"xxxxsd",level:2},
			{id:7 ,name:"分口2",superId:1,remark:"xxxxsd",level:3},
			{id:8 ,name:"分口1",superId:1,remark:"xxxxsd",level:4},
			
			{id:16 ,name:"分口1",superId:2,remark:"xxxxsd",level:1},
			{id:17 ,name:"分口1",superId:2,remark:"xxxxsd",level:3},
			{id:18 ,name:"分口1",superId:2,remark:"xxxxsd",level:4},

			{id:19 ,name:"分口1",superId:7,remark:"xxxxsd",level:1},
			{id:20,name:"分口1",superId:7,remark:"xxxxsd",level:2},

			{id:21,name:"分口1",superId:20,remark:"xxxxsd",level:2}];
			xxx(object,"#treeTable2");
		
	},function(){});//请求数据
	
}

requestPort();
requestSource();
requestSort();
requestClassification();//分级
//向后台请求分级信息
function requestClassification()
{	
	var url="getAllInformationClassification.do";//分级地址
	var data={};
	var object={};
	data=YuXi.util.get(url,"null",function(data){
		object=data.object;
		//用data循环添加分口信息
		for(var i=0;i<object.length;i++)
		{
			var html="<option  value="+object[i].id+" >"+object[i].name+"</option>";
			$(".pes").append(html);
		}

	},function(){
		
	});//请求数据
	
}
//向后台请求分类信息
function requestSort()
{	//数据模拟

	
	
	var url="getAllInfomationSort.do";//分类地址
	var data={};
	var object={};
	data=YuXi.util.get(url,"null",function(data){
		object=data.object;
		//用data循环添加分类信息
		for(var i=0;i<object.length;i++)
		{
			var html="<option  value="+object[i].id+" >"+object[i].name+"</option>";
			$(".sort").append(html);
		}
	},function(){
		
	});//请求数据
	
}


})
